package com.atguigu.tingshu.account.mapper;

import com.atguigu.tingshu.model.account.UserAccount;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;

@Mapper
public interface UserAccountMapper extends BaseMapper<UserAccount> {

    /**
     * 扣减账户余额
     * @param amount
     * @param userId
     */
    @Update("update user_account set available_amount = available_amount - #{amount}, total_amount = total_amount - #{amount}\n" +
            "                      ,total_pay_amount = total_pay_amount + #{amount}\n" +
            "where available_amount >= #{amount} and user_id = #{userId} and is_deleted = 0")
    Long checkAndDeduct(@Param("amount") BigDecimal amount, @Param("userId") Long userId);

    /**
     * 更新账户余额
     * @param amount
     * @param userId
     */
    @Update("update user_account set total_income_amount = total_income_amount + #{amount}, available_amount = available_amount + #{amount}, total_amount = total_amount + #{amount} where user_id = #{userId} and is_deleted = 0")
    void updateAmount(@Param("amount") BigDecimal amount, @Param("userId") Long userId);
}
